__author__ = 'tyrion'

#!/usr/bin/python
import  requests
import  json
import  re
import  numpy as np

#
headers = {'content-type': 'application/json'}
para = [{"server": "flexweb-wx-1", "instances":[{"instance": "flexweb-tomcat-1", "start":"", "end": ""}]}]
url = "http://221.228.81.136:5001/infrastructure/monitor/perdata"
    # print url

r = requests.post(url, data=json.dumps(para), headers=headers)
# print (r.text.split('\n'))
# print(r.text.split('\n')[2:])
# print(re.sub("-nan", "0", r.text).split('\n')[2:])
step = 120
realStep = int(step/60)

sList  = re.sub("-nan", "0", r.text).split('\n')[2:]
print sList
statTime = sList[0].split(':')[0]
print(statTime)

tList = []
timeTupe = ()
valueArry = ()
for ele in sList:
    tempDic = {}
    tmpList = ele.split(':')
    # print(tmpList)
    # tempDic[tmpList[0]] = tmpList[1].strip(' ').split(' ')
    tmpList2 = tmpList[1].strip(' ').split(' ')
    tList.append(tmpList2)
    # print(tempDic)
    # tList.append(tempDic)

print(tList)
floatTList =  [[ float(x) for x in inner] for inner in tList]
print(floatTList)
step = 2
# for  elem in floatTList:
print floatTList.__len__()
print(len(floatTList))

num = int(len(floatTList)/step)
print( num)
result = []
for i in range( 0,  num+1 , step):
    # print( i)
    tmparray = np.array(floatTList[i:i+step])
    print(tmparray)
    # tmparray.mean(axis=0)
    print(tmparray.mean(axis=0))







